package com.bwie.controller;

import com.bwie.domain.BookExport;
import com.bwie.service.BookService;
import com.bwie.service.ImportService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/resource")
@RequiredArgsConstructor
@Slf4j
public class ResourceImportController {

    private final ImportService resourceImportService;

    /**
     * 资源荐购Excel导入接口
     * @param file 上传的Excel文件
     * @return 导入结果
     */
    @PostMapping("/import")
    public ResponseEntity<BookExport> importResource(@RequestParam("file") MultipartFile file) {
        try {
            BookExport BookExport = resourceImportService.importResourceExcel(file);
            return new ResponseEntity<>(BookExport, HttpStatus.OK);
        } catch (Exception e) {
            log.error("资源导入接口异常", e);
            BookExport errorDTO = new BookExport();
            return new ResponseEntity<>(errorDTO, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
